package com.rgs.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.rgs.base.core.model.WebApiReturnResultModel;
import com.rgs.base.core.page.IPageRequestModel;
import com.rgs.base.core.page.IPageResponseModel;
import com.rgs.base.core.system.WebResponseState;
import com.rgs.base.database.DataPageUtil;
import com.rgs.model.platform.vo.BillInfoVO;
import com.rgs.platform.dto.bill.ListBillPageRequestDTO;
import com.rgs.platform.dto.bill.SubmitBillRequestDTO;
import com.rgs.platform.dto.lend.SubmitLendRequestDTO;
import com.rgs.platform.entity.BillInfoDO;
import com.rgs.platform.entity.LendInfoDO;
import com.rgs.platform.filter.BillInfoFilterDTO;
import com.rgs.platform.service.BillInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Ren Gaoshuai
 * @ClassName BillInfoController
 * @date 2022-01-24 11:27
 **/
@RestController
@RequestMapping("/v1/bill")
public class BillInfoController {

    @Resource
    BillInfoService billInfoService;

    @ApiOperation("/获取账单详情")
    @GetMapping("/{id}")
    public WebApiReturnResultModel<BillInfoVO> selectByBillId(@PathVariable("id") String id) {
        BillInfoVO billInfoVO = BeanUtil.copyProperties(billInfoService.selectById(id), BillInfoVO.class);
        return WebApiReturnResultModel.ofSuccess(billInfoVO);
    }

    @ApiOperation("新增账单")
    @PostMapping
    public WebApiReturnResultModel<Void> insert(@RequestBody @Validated SubmitBillRequestDTO submitBillRequest) {
        BillInfoDO billInfoDO = BeanUtil.copyProperties(submitBillRequest, BillInfoDO.class);
        billInfoService.insert(billInfoDO);
        return WebApiReturnResultModel.ofSuccess();
    }

    @ApiOperation(value = "编辑")
    @PatchMapping
    public WebApiReturnResultModel<Void> update(@RequestBody SubmitBillRequestDTO submitBillRequest) {
        //Id不能为空
        if (Strings.isNullOrEmpty(submitBillRequest.getId())) {
            return WebApiReturnResultModel.ofStatus(WebResponseState.PARAMETER_MUST_NOT_NULL);
        }
        BillInfoDO billInfoDO = BeanUtil.copyProperties(submitBillRequest, BillInfoDO.class);
        billInfoService.updateSelectiveByPrimaryKey(billInfoDO);
        return WebApiReturnResultModel.ofSuccess();
    }

    @ApiOperation("删除账单")
    @DeleteMapping("/{id}")
    public WebApiReturnResultModel<Void> delete(@PathVariable("id") String id) {
        billInfoService.deleteById(id);
        return WebApiReturnResultModel.ofSuccess();
    }

    @ApiOperation("分页获取账单")
    @PostMapping("/page")
    public WebApiReturnResultModel<IPageResponseModel<BillInfoVO>> page(
            @RequestBody @Validated ListBillPageRequestDTO listBillPageRequest) {
        IPageResponseModel<BillInfoVO> iPageResponseModel = commonBillPage(listBillPageRequest, listBillPageRequest);
        return WebApiReturnResultModel.ofSuccess(iPageResponseModel);
    }

    private IPageResponseModel<BillInfoVO> commonBillPage(IPageRequestModel iPageRequestModel,
                                                          ListBillPageRequestDTO listBillPageRequest) {
        //获取数据
        PageInfo<BillInfoDO> pageInfo = obtainPageList(iPageRequestModel, listBillPageRequest);

        //DO转VO
        List<BillInfoVO> voList = pageInfo.getList().stream().map(q ->
                BeanUtil.copyProperties(q, BillInfoVO.class)).collect(Collectors.toList());

        return DataPageUtil.buildResponseModel(pageInfo, voList);
    }

    private PageInfo<BillInfoDO> obtainPageList(IPageRequestModel iPageRequestModel,
                                                ListBillPageRequestDTO listBillPageRequest) {
        return billInfoService.listPageByFilter(
                iPageRequestModel,
                BillInfoFilterDTO.builder()
                        .billType(listBillPageRequest.getBillType())
                        .receiveName(listBillPageRequest.getReceiveName())
                        .isAttend(listBillPageRequest.getIsAttend())
                        .orderByClause(listBillPageRequest.getOrderBy())
                        .build()
        );
    }

    @ApiOperation("账单导出")
    @PostMapping("/export")
    public WebApiReturnResultModel<Void> export(
            @RequestBody @Validated ListBillPageRequestDTO listBillPageRequest) {
        return WebApiReturnResultModel.ofSuccess();
    }
}
